import java.text.DecimalFormat;

public class Triangle extends Polygons {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Point x1;
    private Point x2;
    private Point x3;

    public double perimeter() {
        double perimeter = (Math.sqrt((Math.pow((x1.getX() - x2.getX()), 2) + (Math.pow((x1.getY() - x2.getY()), 2))))
                + Math.sqrt((Math.pow((x2.getX() - x3.getX()), 2) + (Math.pow((x2.getY() - x3.getY()), 2)))) + Math.sqrt((Math.pow((x3
                .getX() - x1.getX()), 2) + (Math.pow((x3.getY() - x1.getY()), 2)))));
        return perimeter;
    }

    // pec herona formulas
    public double area() {
        double p = perimeter() / 2;
        double a = Math.sqrt((Math.pow((x1.getX() - x2.getX()), 2) + (Math.pow((x1.getY() - x2.getY()), 2))));
        double b = Math.sqrt((Math.pow((x2.getX() - x3.getX()), 2) + (Math.pow((x2.getY() - x3.getY()), 2))));
        double c = Math.sqrt((Math.pow((x3.getX() - x1.getX()), 2) + (Math.pow((x3.getY() - x1.getY()), 2))));
        double area = Math.sqrt(p * (p - a) + (p - b) + (p - c));
        return area;
    }

    /*
     * Our three points are (0,12), (0,-12), and (1,0). The mean of the
     * x-coordinates is (0+0+1)/3 or 1/3. The mean of the y-coordinates is
     * (12-12+0)/3 or 0. So the centroid's coordinates are (1/3,0).
     */
    public Point getCenter() {
        double x = (x1.getX() + x2.getX() + x3.getX()) / 3;
        double y = (x1.getY() + x2.getY() + x3.getY()) / 3;
        return new Point(x, y);
    }

    public Triangle(Point p1, Point p2, Point p3) {
        this.x1 = p1;
        this.x2 = p2;
        this.x3 = p3;
    }

    public String toString() {
        StringBuilder figureString = new StringBuilder();
        DecimalFormat f = new DecimalFormat(Figures.pattern);
        figureString.append("Point1:").append("x:").append(f.format(x1.getX())).append(" ").append("y:").append(f.format(x1.getY()))
                .append(" ").append("Point2:").append("x:").append(f.format(x2.getX())).append(" ").append("y:")
                .append(f.format(x2.getY())).append("Point3:").append("x:").append(f.format(x3.getX())).append(" ").append("y:").append(
                        f.format(x3.getY())).append(" ");
        figureString.append("Perimeter:").append(f.format(this.perimeter())).append("Area:").append(f.format(this.area())).append(
                "CenterX:").append(f.format(this.getCenter().getX())).append(" ").append("CenterY:").append(
                f.format(this.getCenter().getY()));
        String triangle = new String(figureString);
        return triangle;
    }

}
